import axios from 'axios';
import { AlertModule } from 'vux'

var baseURL = ''
if(location.hostname == 'localhost'){
  baseURL = process.env.BASE_API
}else if(location.hostname == 'atest.xinxinshopping.com'){
  baseURL = process.env.API_PATH_TEST
}else if(location.hostname == 'ngrok.codeliker.com'){
  baseURL = process.env.API_PATH_TEST
}

const instance = axios.create({
    baseURL: baseURL,
    timeout: 30000,
});
// request拦截器
// service.interceptors.request.use(
//   config => {
//     if (store.getters.token) {
//       config.headers['X-Token'] = getToken() // 让每个请求携带自定义token 请根据实际情况自行修改
//     }
//     return config
//   },
//   error => {
//     // Do something with request error
//     console.log(error) // for debug
//     Promise.reject(error)
//   }
// )


instance.interceptors.response.use((response) => {
    //返回状态码是否为200
  if (response.data.code == 0) {
    return response;
    } else  {
        AlertModule.show({
          title: '错误！',
          content: response.data.msg,
        })
        console.log(`network error: ${response.data.msg}`);
        return Promise.reject(response.data.msg);
    }
},(error) => {
    // Do something with response error
    AlertModule.show({
      title: '错误！',
      content: error.response.statusText,
    })
    return Promise.reject(error);
});

export default instance;
